//
//  ShortViewController.m
//  CJMobileAdDemo
//
//  Created by Jun on 2022/4/11.
//

#import "ShortViewController.h"
#import <CJMobileAd/CJMobileAd.h>
#import "CJMobilePublic.h"

@interface ShortViewController ()<CJShortVideoDelegate>

@property (nonatomic, strong) CJShortVideoAd *shortVideoAd;
@property (nonatomic, strong) UIView *contentView;

@end

@implementation ShortViewController

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}


- (void)dealloc
{
    NSLog(@"the page of fullscreenVideo delloc");
}

- (void)loadAdData {
    [self.customView canShowStatus:false errorMsg:@""];
    self.shortVideoAd = [[CJShortVideoAd alloc] initWithSlotId:shortVideoId];
    self.shortVideoAd.delegate = self;
    [self.shortVideoAd loadAdData];
}

- (void)setupUI {
    self.title = @"shortVideo";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;
    [self.view addSubview:self.contentView];
    
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        [self loadAdData];
        return;
    }
    self.contentView.hidden = false;
    [self.view bringSubviewToFront:self.contentView];
    [self.shortVideoAd showFromRootViewController:self contentView:self.contentView];
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak ShortViewController *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.hidden = true;
        _contentView.frame = CGRectMake(0, self.navigationController.navigationBar.frame.size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        _contentView.clipsToBounds = true;
    }
    return _contentView;
}

#pragma mark CJFullscreenVideoAdDelegate

- (void)shortAdDidLoad:(CJShortVideoAd *)shortVideoAd resourceId:(NSString *)resourceId {
    NSLog(@"load success");
    [self.customView canShowStatus:true errorMsg:@""];
}

- (void)shortVideoAdLoadFailed:(CJShortVideoAd *)shortVideoAd error:(NSError *)error {
    NSLog(@"load failed of error: %@", error);
    [self.customView canShowStatus:false errorMsg:error.description];
}

- (void)shortVideoStateChange:(CJShortVideoStatus)status error:(NSError *)error {
    switch (status) {
        case CJShortVideoPlaying:
            NSLog(@"播放中");
            break;
        case CJShortVideoPause:
            NSLog(@"暂停");
            break;
        case CJShortVideoStop:
            NSLog(@"停止");
        case CJShortVideoError:
            NSLog(@"播放错误");
            break;
        case CJShortVideoEnd:
            NSLog(@"播放结束");
            break;
        case CJShortVideoUnKnow:
            NSLog(@"未知状态");
            break;
        default:
            
            break;
    }
}

@end
